package com.jstyle.beatecg.util;

import android.util.Log;
import java.io.UnsupportedEncodingException;


/**
 * Created by Administrator on 2017/4/6.
 */

public class ResolveData {
    private static final String TAG = "ResolveData";
    private static final int SHORTENED_LOCAL_NAME = 0x08;
    private static final int COMPLETE_LOCAL_NAME = 0x09;
    private static final long oneMinMillis = 60 * 1000l;

    /**bcd格式的时间
     * @param value
     * @return
     */
    public static int getBcd(String value) {
        Integer m = Integer.parseInt(value, 16);
        return m.intValue();
    }

    public static String decodeDeviceName(byte[] data) {
        String name = null;
        int fieldLength, fieldName;
        int packetLength = data.length;
        for (int index = 0; index < packetLength; index++) {
            fieldLength = data[index];
            if (fieldLength == 0)
                break;
            fieldName = data[++index];

            if (fieldName == COMPLETE_LOCAL_NAME
                    || fieldName == SHORTENED_LOCAL_NAME) {
                name = decodeLocalName(data, index + 1, fieldLength - 1);
                break;
            }
            index += fieldLength - 1;
        }
        return name;
    }

    /**
     * Decodes the local name
     */
    public static String decodeLocalName(final byte[] data, final int start,
                                         final int length) {
        try {
            return new String(data, start, length, "UTF-8");
        } catch (final UnsupportedEncodingException e) {
            Log.e("scan", "Unable to convert the complete local name to UTF-8",
                    e);
            return null;
        } catch (final IndexOutOfBoundsException e) {
            Log.e("scan", "Error when reading complete local name", e);
            return null;
        }
    }

    /**
     * 转十六进制字符串
     *
     * @param data
     * @return
     */
    public static String byte2Hex(byte[] data) {
        if (data != null && data.length > 0) {
            StringBuilder sb = new StringBuilder(data.length);
            for (byte tmp : data) {
                sb.append(String.format("%02X ", tmp));
            }
            return sb.toString();
        }
        return "no data";
    }



    public static int getValue(byte b, int count) {
        return (int) ((b & 0xff) * Math.pow(256, count));
    }


    /**设备版本号
     * @param value
     * @return
     */
    public static String getVersion(byte value) {
        return String.format("%X", value);
    }


    public static byte[] getByteArray(byte b) {
        byte[] array = new byte[8];
        for (int i = 7; i >=0; i--) {
            array[i] = (byte) (b & 1);
            b = (byte) (b >> 1);
        }
        return array;
    }

    /**过滤防溢出
     * @param high
     * @param low
     * @return
     */
    public static byte getByteArray(byte high,byte low) {
        byte data=0;
        byte[]temp=new byte[8];
        byte[] arrayHigh = getByteArray(high);
        byte[] arrayLow = getByteArray(low);
        System.arraycopy(arrayHigh,4,temp,0,4);
        System.arraycopy(arrayLow,0,temp,4,4);
        for(int i=0;i<temp.length;i++){
            if(temp[i]==1){
                data+=Math.pow(2,7-i);
            }
        }
        return data;
    }

    /**
     * 16进制转10进制
     * @param shiliu
     * @return
     */
    public static Integer ToStringSHI(String shiliu){
      return   Integer.valueOf(shiliu,16);
    }

}
